package Day28;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Demo03 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<Person> aClass = (Class<Person>) Class.forName("Day28.Person");
        /*//找到无参构造方法
        Constructor<Person> c1 = aClass.getConstructor();
        //使用无参构造方法创建对象
        Person p = c1.newInstance();
        System.out.println(p);*/

        /*//找到包含String name和int age 的构造方法
        Constructor<Person> c2 = aClass.getConstructor(String.class, int.class);
        //使用有参构造方法创建对象
        Person p = c2.newInstance("李四",18);//一但是有参构造，创建时需要传参数,不然会报错
        System.out.println(p);*/

        //如何让用反射技术打破对象的私有类型
        Constructor<Person> c3 = aClass.getDeclaredConstructor(String.class);
        c3.setAccessible(true);//这一步便是可以忽略权限检查
        Person p = c3.newInstance("张三");
        System.out.println(p);

    }
}